﻿using System.Collections;
using System.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Web.Syndication.Atom
{
    public class AtomItem : IFeedItem
    {

        public string ID { get { return this._id; } set { this._id = value; } }
        public string Title { get { return this._title; } set { this._title = value; } }
        public List<string> Category { get { return this._Category; } }
        public Uri Link { get { return this._link; } set { this._link = value; } }
        public string Description { get { return this._summary; } set { this._summary = value; } }
        public string Content { get { return this._content; } set { this._content = value; } }
        public DateTime PublishDate { get { return this._published; } set { this._published = value; } }
        public DateTime Updated { get { return this._updated; } set { this._updated = value; } }
        public List<AtomAuthor> Author { get { return this._Author; } }
        public List<AtomAuthor> Contributor { get { return this._contributor; } }

        private List<AtomAuthor> _Author = null;
        private List<AtomAuthor> _contributor = null;
        private List<string> _Category = null;
        private string _content = string.Empty;
        private string _id = string.Empty;
        private string _title = string.Empty;
        private Uri _link = null;
        private string _summary = string.Empty;
        private DateTime _updated = DateTime.MinValue;
        private DateTime _published = DateTime.MinValue;

        public AtomItem()
        {
            this._contributor = new List<AtomAuthor>();
            this._Author = new List<AtomAuthor>();
            this._Category = new List<string>();
            this._published = DateTime.MinValue;
            this._content = string.Empty;
            this._id = Guid.NewGuid().ToString();
            this._title = string.Empty;
            this._summary = string.Empty;
            this._link = null;
            this._updated = DateTime.Now;
        }

        public AtomItem(string ID, string Title, string Summary, string Content, Uri Link, DateTime Published, DateTime Updated)
        {
            this._contributor = new List<AtomAuthor>();
            this._Author = new List<AtomAuthor>();
            this._Category = new List<string>();
            this._published = Published;
            this._content = Content;
            this._id = ID;
            this._title = Title;
            this._summary = Summary;
            this._link = Link;
            this._updated = Updated;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<entry>");
            sb.AppendLine(string.Format("<id>{0}</id>", FeedHelper.XmlEncode(this._id)));
            sb.AppendLine(string.Format("<title>{0}</title>", FeedHelper.XmlEncode(this._title)));

            if (this._link != null)
            {
                sb.AppendLine(string.Format("<link>{0}</link>", FeedHelper.XmlEncode(this._link.OriginalString)));
            }
            else
            {
                sb.AppendLine("<link></link>");
            }

            if (this._Category.Count > 0)
            {
                foreach (string s in this._Category)
                {
                    sb.AppendLine(string.Format("<category term=\"{0}\" />", s));
                }
            }

            sb.AppendLine(string.Format("<summary>{0}</summary>", FeedHelper.XmlEncode(this._summary)));
            sb.AppendLine(string.Format("<content>{0}</content>", FeedHelper.XmlEncode(this._content)));
            sb.AppendLine(string.Format("<updated>{0}</updated>", this._updated.ToString("r")));
            sb.AppendLine("</entry>");
            return sb.ToString();
        }

        public XmlNode ToXml(XmlDocument Document)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, "entry", "http://www.w3.org/2005/Atom");
            node.AppendChild(FeedHelper.CreateElement(Document, "id", this._id, "http://www.w3.org/2005/Atom"));
            node.AppendChild(FeedHelper.CreateElement(Document, "title", this._title, "http://www.w3.org/2005/Atom"));

            if (this._link != null)
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "link", this._link.OriginalString, "http://www.w3.org/2005/Atom"));
            }
            else
            {
                node.AppendChild(FeedHelper.CreateElement(Document, "link", string.Empty, "http://www.w3.org/2005/Atom"));
            }

            node.AppendChild(FeedHelper.CreateElement(Document, "published", this._published.ToString("r"), "http://www.w3.org/2005/Atom"));
            node.AppendChild(FeedHelper.CreateElement(Document, "updated", this._updated.ToString("r"), "http://www.w3.org/2005/Atom"));
            node.AppendChild(FeedHelper.CreateElement(Document, "summary", this._summary, "http://www.w3.org/2005/Atom"));
            node.AppendChild(FeedHelper.CreateElement(Document, "content", this._content, "http://www.w3.org/2005/Atom"));

            if (this._Category.Count > 0)
            {
                foreach (string s in this._Category)
                {
                    XmlElement item = FeedHelper.CreateElement(Document, "category", string.Empty, "http://www.w3.org/2005/Atom");
                    item.Attributes.Append(FeedHelper.CreateAttribute(Document, "term", s));
                    node.AppendChild(item);
                }
            }

            if (this._Author.Count > 0)
            {
                foreach (AtomAuthor Author in this._Author)
                {
                    node.AppendChild(Author.ToXml(Document));
                }
            }

            if (this._contributor.Count > 0)
            {
                foreach (AtomAuthor Author in this._contributor)
                {
                    node.AppendChild(Author.ToXml(Document, "contributor"));
                }
            }

            return node;
        }

    }
}
